from sqlalchemy import Column, Integer, String, Text, ForeignKey, Boolean, DateTime
from sqlalchemy.orm import relationship
from datetime import datetime
from app.db.database import Base

class User(Base):
    __tablename__ = "users"
    
    id = Column(Integer, primary_key=True, index=True)
    username = Column(String(50), unique=True, index=True, nullable=False)
    email = Column(String(100), unique=True, index=True, nullable=False)
    hashed_password = Column(String(255), nullable=False)
    is_active = Column(Boolean, default=True)
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    # 关系
    resumes = relationship("Resume", back_populates="owner")

class Resume(Base):
    __tablename__ = "resumes"
    
    id = Column(Integer, primary_key=True, index=True)
    title = Column(String(100), nullable=False)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    template_type = Column(String(50), default="default")
    is_active = Column(Boolean, default=True)
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    # 关系
    owner = relationship("User", back_populates="resumes")
    personal_info = relationship("PersonalInfo", back_populates="resume", uselist=False)
    education = relationship("Education", back_populates="resume")
    work_experience = relationship("WorkExperience", back_populates="resume")
    skills = relationship("Skill", back_populates="resume")
    certificates = relationship("Certificate", back_populates="resume")

class PersonalInfo(Base):
    __tablename__ = "personal_info"
    
    id = Column(Integer, primary_key=True, index=True)
    resume_id = Column(Integer, ForeignKey("resumes.id"), nullable=False)
    full_name = Column(String(100), nullable=False)
    phone = Column(String(20))
    email = Column(String(100))
    address = Column(String(255))
    summary = Column(Text)
    
    # 关系
    resume = relationship("Resume", back_populates="personal_info")

class Education(Base):
    __tablename__ = "education"
    
    id = Column(Integer, primary_key=True, index=True)
    resume_id = Column(Integer, ForeignKey("resumes.id"), nullable=False)
    institution = Column(String(100), nullable=False)
    degree = Column(String(50), nullable=False)
    major = Column(String(100))
    start_date = Column(String(20))
    end_date = Column(String(20))
    description = Column(Text)
    
    # 关系
    resume = relationship("Resume", back_populates="education")

class WorkExperience(Base):
    __tablename__ = "work_experience"
    
    id = Column(Integer, primary_key=True, index=True)
    resume_id = Column(Integer, ForeignKey("resumes.id"), nullable=False)
    company = Column(String(100), nullable=False)
    position = Column(String(100), nullable=False)
    start_date = Column(String(20))
    end_date = Column(String(20))
    description = Column(Text)
    achievements = Column(Text)
    
    # 关系
    resume = relationship("Resume", back_populates="work_experience")

class Skill(Base):
    __tablename__ = "skills"
    
    id = Column(Integer, primary_key=True, index=True)
    resume_id = Column(Integer, ForeignKey("resumes.id"), nullable=False)
    name = Column(String(50), nullable=False)
    level = Column(Integer, default=1)  # 1-5表示熟练度
    
    # 关系
    resume = relationship("Resume", back_populates="skills")

class Certificate(Base):
    __tablename__ = "certificates"
    
    id = Column(Integer, primary_key=True, index=True)
    resume_id = Column(Integer, ForeignKey("resumes.id"), nullable=False)
    name = Column(String(100), nullable=False)
    issuing_authority = Column(String(100))
    issue_date = Column(String(20))
    expiry_date = Column(String(20))
    
    # 关系
    resume = relationship("Resume", back_populates="certificates")

class JobDescription(Base):
    __tablename__ = "job_descriptions"
    
    id = Column(Integer, primary_key=True, index=True)
    title = Column(String(100), nullable=False)
    company = Column(String(100))
    content = Column(Text, nullable=False)
    industry = Column(String(50))
    job_type = Column(String(50))  # 如：互联网产品岗、金融风控岗、教育教学岗等
    key_skills = Column(Text)  # AI提取的关键技能
    created_at = Column(DateTime, default=datetime.utcnow)